/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import BE.Artist;
import BE.Category;
import BE.Song;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author a.tamas
 */
public class SearchDBManager {
    private static SearchDBManager instance = null;
    private final DBConnectionManager cm;    
    private final ArtistDBManager adb;
    private final CategoryDBManager cdb;
    private final SongsDBManager sdb;
    
    public static SearchDBManager getInstance() throws IOException
    {
        if(instance == null)
        {
            instance = new SearchDBManager();
        }
        return instance;
    }
    
    private SearchDBManager() throws IOException
    {
        cm = DBConnectionManager.getInstance();
        adb = ArtistDBManager.getInstance();
        sdb = SongsDBManager.getInstance();
        cdb = CategoryDBManager.getInstance();
    }

    public ArrayList<Song> activateSearch(String artistSelection, String genreSelection, String searchValue) throws SQLException 
    {
       
        ArrayList<Song> sngList = new ArrayList<>();
        
        try (Connection con = cm.getConnection())
        {
        String sql = "Select s.id, s.title, s.artistId, s.categoryId, s.bpm, s.duration, s.favourite from song s, artist a, category c where a.name = ? or c.name = ? or s.title = ?";
        PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, artistSelection);
            ps.setString(2, genreSelection);
            ps.setString(3, searchValue);

            ResultSet rs = ps.executeQuery();
            
            while(rs.next())
            {
                Song sn = sdb.getOneSong(rs);
                sngList.add(sn);
            }
        }
        return sngList;
    }
    
    
    
}
